package com.bw.kotlindemo.java.fragment;

import android.view.View;
import android.widget.ImageView;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bw.kotlindemo.R;
import com.bw.kotlindemo.databinding.FragmentLiveListBinding;
import com.bw.kotlindemo.java.adapter.LiveListAdapter;
import com.bw.kotlindemo.java.base.BaseFragment;
import com.bw.kotlindemo.java.model.bean.LiveListBean;
import com.bw.kotlindemo.java.model.bean.TypeBean;
import com.bw.kotlindemo.java.vm.LiveVM;

import java.util.ArrayList;
import java.util.List;

/**
 * desc:直播列表fragment
 * create by xushaodong on 2023/3/22
 */
public class LiveListFragment extends BaseFragment<LiveVM, FragmentLiveListBinding> {
    private TypeBean.ResultBean typeResultBean;
    private LiveListAdapter adapter;
    private int page = 1,size = 10;
    private List<LiveListBean.ResultBean> data = new ArrayList<>();

    public LiveListFragment(TypeBean.ResultBean typeResultBean) {
        this.typeResultBean = typeResultBean;
    }

    @Override
    protected void initData() {
        setView();
        initVM();
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_live_list;
    }

    private void initVM() {
        //获取网络直播列表
        loadData();
        mViewModel.liveListVM.observe(this, new Observer<LiveListBean>() {
            @Override
            public void onChanged(LiveListBean homeListBean) {
                //收到网络直播列表结果，更新UI
                if(homeListBean != null && homeListBean.getResult() != null){
                    //第一种写法
                    if(page == 1){
                        //下拉刷新回来的数据
                        data.clear();
                    }
                    data.addAll(homeListBean.getResult());
                    adapter.setData(homeListBean.getResult());

                    //优化的写法

//                    if(page == 1){
//                        data.clear();
//                        data.addAll(homeListBean.getResult());
//                        adapter.setData(data);
//                    }else {
//                        //加载更多
//                        adapter.addData(homeListBean.getResult());
//                    }


//                    adapter.setData(homeListBean.getResult());
//                    liveList2Adapter.notifyDataSetChanged();
                }
            }
        });
    }

    /**
     * 获取网络数据
     */
    private void loadData() {
        mViewModel.loadLiveList(typeResultBean.getId(),page,size);
    }

    private void setView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, 2);
        mDataBinding.rvLiveList.setLayoutManager(gridLayoutManager);

        adapter = new LiveListAdapter(activity);
        mDataBinding.rvLiveList.setAdapter(adapter);

//        liveList2Adapter = new LiveList2Adapter(activity,R.layout.item_live_list,data);
//        mDataBinding.rvLiveList.setAdapter(new CommonAdapter<HomeListBean.ResultBean>(activity,R.layout.item_live_list,data) {
//            @Override
//            protected void convert(ViewHolder holder, HomeListBean.ResultBean resultBean, int position) {
//                holder.setText(R.id.tv_title,resultBean.getNickName());
//                ImageView imageView = holder.getView(R.id.img);
//                ImgLoadUtils.loadImg(activity,resultBean.getLiveCoverUrl(),imageView);
//            }
//        });

    }
}
